package cn.bore.tomato.service.impl;

import cn.bore.common.core.utils.MapstructUtils;
import cn.bore.common.core.utils.StringUtils;
import cn.bore.common.mybatis.core.page.TableDataInfo;
import cn.bore.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.bore.tomato.domain.bo.TmsTasksBo;
import cn.bore.tomato.domain.vo.TmsTasksVo;
import cn.bore.tomato.domain.TmsTasks;
import cn.bore.tomato.mapper.TmsTasksMapper;
import cn.bore.tomato.service.ITmsTasksService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 测试单Service业务层处理
 *
 * @author 范亚鹏
 * @date 2025-08-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TmsTasksServiceImpl implements ITmsTasksService {

    private final TmsTasksMapper baseMapper;

    /**
     * 查询测试单
     *
     * @param id 主键
     * @return 测试单
     */
    @Override
    public TmsTasksVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询测试单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 测试单分页列表
     */
    @Override
    public TableDataInfo<TmsTasksVo> queryPageList(TmsTasksBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TmsTasks> lqw = buildQueryWrapper(bo);
        Page<TmsTasksVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的测试单列表
     *
     * @param bo 查询条件
     * @return 测试单列表
     */
    @Override
    public List<TmsTasksVo> queryList(TmsTasksBo bo) {
        LambdaQueryWrapper<TmsTasks> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TmsTasks> buildQueryWrapper(TmsTasksBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TmsTasks> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TmsTasks::getId);
        lqw.eq(bo.getUserId() != null, TmsTasks::getUserId, bo.getUserId());
        lqw.eq(bo.getWorkDuration() != null, TmsTasks::getWorkDuration, bo.getWorkDuration());
        lqw.eq(bo.getRestDuration() != null, TmsTasks::getRestDuration, bo.getRestDuration());
        lqw.eq(bo.getRepeatTime() != null, TmsTasks::getRepeatTime, bo.getRepeatTime());
        lqw.eq(bo.getOrderNum() != null, TmsTasks::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getCompleted() != null, TmsTasks::getCompleted, bo.getCompleted());
        return lqw;
    }

    /**
     * 新增测试单
     *
     * @param bo 测试单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TmsTasksBo bo) {
        TmsTasks add = MapstructUtils.convert(bo, TmsTasks.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改测试单
     *
     * @param bo 测试单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TmsTasksBo bo) {
        TmsTasks update = MapstructUtils.convert(bo, TmsTasks.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TmsTasks entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除测试单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
